package cn.donting.jme.swing;

import cn.donting.jme.expansion.attribute.ValueChangeListener;
import cn.donting.jme.swing.layout.Anchor;
import cn.donting.jme.swing.layout.AnchorLayout;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import org.jdesktop.swingx.VerticalLayout;
import org.jdesktop.swingx.WrapLayout;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class JTitlePane extends JPanel implements ContainerListener {
    /**
     * 标题Panel
     */
    private JPanel titlePanel;
    /**
     * contentPanel
     */
    private JKeyValuePanel contentPanel;
    /**
     * 标题的 Label
     */
    private JLabel titleJLabel;
    /**
     * 展开  icon ,
     */
    private JLabel expandJLabel;
    /**
     *
     */
    private JCheckBox enabledCheckBox;

    private ValueChangeListener<Boolean> enabledValueChange;

    public JTitlePane() {
        titlePanel = new JPanel();
        titlePanel.setLayout(new AnchorLayout());
        titleJLabel = new JLabel("title");
        titlePanel.add(titleJLabel,new Anchor(1,2,null,null));
        contentPanel = new JKeyValuePanel();
        setLayout(new BorderLayout());
        add(titlePanel, BorderLayout.NORTH);
        add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new VerticalLayout());


        Color color = UIManager.getColor("Component.borderColor");
        Border border = BorderFactory.createMatteBorder(1, 1, 1, 1, color);
        setBorder(border);
        titlePanel.setBackground(color);
        titlePanel.setBorder(BorderFactory.createMatteBorder(3, 2, 3, 2, color));


        expandJLabel = new JLabel();
        titlePanel.add(expandJLabel, new Anchor(1, null, 1, null));
        expandJLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1 && SwingUtilities.isLeftMouseButton(e)) {
                    setExpand(!contentPanel.isVisible());
                }

            }
        });
        titlePanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1 && SwingUtilities.isLeftMouseButton(e)) {
                    setExpand(!contentPanel.isVisible());
                }
            }
        });
        titlePanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
                titlePanel.setCursor(cursor);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                Cursor cursor = Cursor.getDefaultCursor();
                titlePanel.setCursor(cursor);
            }
        });
        enabledCheckBox = new JCheckBox();
        enabledCheckBox.setSelected(false);
        enabledCheckBox.addChangeListener(e -> {
            if (enabledValueChange != null) {
                enabledValueChange.change(enabledCheckBox.isSelected());
            }
        });
        contentPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
        setShowEnabled(true);
        setExpand(false);

        contentPanel.addContainerListener(this);
    }


    private void updateSize() {
        JScrollPane scrollPane = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this);
        if (scrollPane != null) {
            JViewport viewport = scrollPane.getViewport();
            Component view = viewport.getView();
            if (view instanceof Container) {
                LayoutManager layout = ((Container) view).getLayout();
                Dimension dimension = layout.preferredLayoutSize((Container) view);
                view.setPreferredSize(new Dimension(0, dimension.height));
            }
        }
    }

    public void setExpand(boolean expand) {
        contentPanel.setVisible(expand);
        updateSize();
        if (expand) {
            expandJLabel.setIcon(new FlatSVGIcon("icon/arrowDown.svg"));
        } else {
            expandJLabel.setIcon(new FlatSVGIcon("icon/arrowUp.svg"));
        }
    }

    public void setShowEnabled(boolean showEnabled) {
        if (showEnabled) {
            titlePanel.add(this.enabledCheckBox, new Anchor(1, 2, null, null));
            titlePanel.remove(this.titleJLabel);
            titlePanel.add(this.titleJLabel, new Anchor(2, 20, null, null));
        } else {
            titlePanel.remove(this.enabledCheckBox);
            titlePanel.remove(this.titleJLabel);
            titlePanel.add(this.titleJLabel,new Anchor(1, 2, null, null));
        }
    }

    public void setEnabled(boolean enabled) {
        this.enabledCheckBox.setSelected(enabled);
        JScrollPane scrollPane = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this);
        if (scrollPane != null) {
            JViewport viewport = scrollPane.getViewport();
            Component view = viewport.getView();
            if (view instanceof Container) {
                LayoutManager layout = ((Container) view).getLayout();
                Dimension dimension = layout.preferredLayoutSize((Container) view);
                view.setPreferredSize(new Dimension(0, dimension.height));
            }
        }

        if (enabledValueChange != null) {
            enabledValueChange.change(enabled);
        }
    }

    public void setEnabledValueChange(ValueChangeListener<Boolean> enabledValueChange) {
        this.enabledValueChange = enabledValueChange;
    }

    public void setTitleIcon(Icon icon) {
        titleJLabel.setIcon(icon);
    }

    public void setTitle(String title) {
        titleJLabel.setText(title);
    }

    public String getTitle() {
        return titleJLabel.getText();
    }

    public JKeyValuePanel getContentPanel() {
        return contentPanel;
    }

    @Override
    public void componentAdded(ContainerEvent e) {
        updateSize();
    }

    @Override
    public void componentRemoved(ContainerEvent e) {
        updateSize();
    }
}
